package com.gitee.dolt.core.validator;


import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * @author LYH
 */
public class SimpleCharacterConstraintValidator implements ConstraintValidator<SimpleCharacter, String> {

    private static Pattern pattern = Pattern.compile("^[a-zA-Z0-9_]*$");

    @Override
    public void initialize(SimpleCharacter constraintAnnotation) {
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StrUtil.isEmpty(value)) {
            return true;
        }
        return ReUtil.isMatch(pattern, value);
    }

}